package cn.bopomo.dealer.base.db.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import cn.bopomo.dealer.base.db.BaseDAO;
import cn.bopomo.dealer.base.db.IAuthorizeDAO;
import cn.bopomo.dealer.base.exception.ExceptionCode;
import cn.bopomo.dealer.base.exception.SystemException;
import cn.bopomo.dealer.base.exception.UserException;
import cn.bopomo.dealer.base.util.Page;
import cn.bopomo.dealer.base.util.ResultCode;
import cn.bopomo.dealer.base.vo.AuthorizeList;
import cn.bopomo.dealer.base.vo.ModuleList;
import cn.bopomo.dealer.base.vo.OperatorDetail;

public class AuthorizeDAOImpl extends BaseDAO implements IAuthorizeDAO {
	
	
	public String insertAuthorize(AuthorizeList authorize) {
		try {
			this.insertOrUpdate(authorize);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			this.logger.warn(e);
			return ResultCode.OP_FAILED;
		}
		return ResultCode.OP_SUCCESS;
	}

	public String updateAuthorize(AuthorizeList authorize) {
		try {
			this.insertOrUpdate(authorize);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			this.logger.warn(e);
			return ResultCode.OP_FAILED;
		}
		return ResultCode.OP_SUCCESS;
	}

	public String deleteAuthorize(AuthorizeList authorize) {
		try {
			this.getHibernateTemplate().delete(authorize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			this.logger.warn(e);
			return ResultCode.OP_FAILED;
		}
		return ResultCode.OP_SUCCESS;
	}

	public AuthorizeList selectAuthorize(String tag) throws SystemException {
		String hql = "from AuthorizeList ";
		Object authorize = null;
		try {
			authorize = this.sectionFirstObject(hql,"where authorizeTag = ?", tag);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new SystemException(ExceptionCode.S_100000,"数据库发生数据异常");
		}
		if (authorize != null){
			return (AuthorizeList)authorize;
		}
		return null;
		
	}

	public AuthorizeList selectAuthorize(int uId) throws SystemException {
		String hql = "from AuthorizeList ";
		Object authorize = null;
		try {
			OperatorDetail od = new OperatorDetail();
			od.setId(new Integer(uId));
			authorize = this.sectionFirstObject(hql,"where operatorDetail = ?", od);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new SystemException(ExceptionCode.S_100000,"数据库发生数据异常");
		}
		if (authorize != null){
			return (AuthorizeList)authorize;
		}
		return null;
	}

	public AuthorizeList selectAuthorize(Integer aId) throws SystemException {
		String hql = "from AuthorizeList ";
		Object authorize = null;
		try {
			authorize = this.sectionFirstObject(hql,"where aid = ?", aId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new SystemException(ExceptionCode.S_100000,"数据库发生数据异常");
		}
		if (authorize != null){
			return (AuthorizeList)authorize;
		}
		return null;
	}
	
	public AuthorizeList selectAuthorizeByAction(String action) throws SystemException {
		String hql = "from AuthorizeList ";
		Object authorize = null;
		try {
			authorize = this.sectionFirstObject(hql,"where authorizeAction = ?", action);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new SystemException(ExceptionCode.S_100000,"数据库发生数据异常");
		}
		if (authorize != null){
			return (AuthorizeList)authorize;
		}
		return null;
	}
	
	public AuthorizeList selectAuthorizeByName(String name,int moduleId) throws SystemException {
		String hql = "from AuthorizeList al ";
		Object authorize = null;
		try {
			authorize = this.sectionFirstObject(hql,"where al.authorizeName = ? and al.moduleList.mid = " + moduleId, name);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SystemException(ExceptionCode.S_100000,"数据库发生数据异常");
		}
		if (authorize != null){
			return (AuthorizeList)authorize;
		}
		return null;
		
	}

	public List selectAuthorize() throws SystemException {
		String hql = "from AuthorizeList";
		return this.selectAll(hql,null);
	}

	public Page selectAuthorize(int pageSize, int rowsPerPage) throws Exception {
		String hql = "from AuthorizeList";
		return this.selectSectionData(hql,null,null,null,"modifyDate",true,pageSize,rowsPerPage);
	}

	public List selectAuthorize(ModuleList module) throws SystemException {
		String hql = "from AuthorizeList au where au.moduleList.mid = " + module.getMid().toString() + " Order By au.addDate desc";
		return this.selectAll(hql,null);
	}
	
	public List selectAuthorizeByTags(final String tagList){
		
		final String sql = "from AuthorizeList where authorize_tag in (?)";
		
		return this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(sql);
				
				query.setParameter(0, tagList);

				return query.list();
			}
		});
		
	}

	/**
	 * 查询数据库里在特定功能模块内，查找特定名称的权限
	 * @param name          特定名称参数
	 * @param tag			特定标签参数
	 * @param id			特定id参数
	 * @return
	 * @throws SystemException
	 */
	public AuthorizeList selectAuthorizeByNameOrTagNotId(String name,String tag,Integer id) throws SystemException{
		final String hql = "from AuthorizeList au where (au.authorizeName = ? or au.authorizeTag = ? ) and au.aid<>?";
		Object[] params = {name,tag,id};
		try {
			return (AuthorizeList)this.selectFirstObject(hql,params);
		} catch (Exception e) {
			throw new SystemException(ExceptionCode.S_100000,"数据库发生数据异常");
		}
	}
	
	
}
